package com.didi.gulimall.product.controller;

import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.product.entity.CategoryEntity;
import com.didi.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品三级分类
 *
 * @author didi
 * @email 1062664904@qq.com
 * @date 2021-01-14 15:42:45
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @GetMapping("/get/tree")
    public R getListWithTree() {
        List<CategoryEntity> category = categoryService.getListWithTree();
        return R.ok().put("level", category);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 批量修改
     *
     * @param categorys
     * @return
     */
    @PostMapping("/update/list")
    public R updateList(@RequestBody List<CategoryEntity> categorys) {
        categoryService.updateBatchById(categorys);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCategory(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody List<Long> catIds) {
        categoryService.removeByIds(catIds);
        return R.ok();
    }

}
